TMPDIR = $$HOME/tmp/disser
TMPDIR2SVN = $$HOME/tmp/disser/SVN

ALL_FILES = Makefile \
	    $(TEX_SOURCES) \
	    $(FIGURES) \
	    $(BIBS) \
            $(RST)  \
            $(PYS) \
            rst2pdfNoErrors rst2pdfNoErrors_v2

FILE=thesis_v2


Dirs		:=${shell find . -type d | grep -v ".svn" | grep  "./"}
TEX_SOURCES	:=${shell  find .  -iname "*.tex" }
FIGURES		:=${shell find .  -iname "*.eps" }
BIBS		:=${shell find .  -iname "*.bib" }
PYS		:=${shell find .  -iname "*.py" }
RST		:=${shell find .  -iname "*.rst" }  
TeXFromRST	:=$(foreach rstfile,$(RST),$(patsubst,%.tex,%.rst,$(rstfile)))		
MAKEFILE	:=Makefile
SCRIPTS		:=${shell find . -iname "rst2pdfNoErrors*" }
SCRIPTS2	:=${shell find . -iname "*.sh" }
CURDIR		:=${shell pwd }


### SVN repo settings
SVNREPO 	:= https://my-code-iggy-floyd-de.googlecode.com/svn/branches
SVNUSER	       	:= iggy.floyd.de@gmail.com

### GIT repo settings
GITREPOPATH     := https://api.github.com/user/repos
GITREPO         := $(FILE)
TMPDIR2GIT      := $$HOME/tmp/$(FILE)/GIT
GITUSER         :=   igormarfin



all : $(FILE).pdf

#$(FILE).pdf: $(FILE).ps
#	ps2pdf $(FILE).ps

#$(FILE).ps : $(FILE).dvi
#	dvips -t a4 -f <$(FILE).dvi >$(FILE).ps

#$(FILE).dvi :$(TEX_SOURCES) \


$(FILE).pdf :$(TEX_SOURCES) \
    $(FIGURES) \
    $(BIBS) \
    $(RST) \
    $(TeXFromRST) 
	- cat rst2pdfNoErrors | bash
	- ./creatPdf 
	- mv main.pdf $(FILE).pdf

#	echo "latex  -interaction=batchmode   main.tex" |sh
#	bibtex main.aux
#	echo "latex  -interaction=batchmode  main.tex" | sh
#	echo "pdflatex  -interaction=batchmode  main.tex" | sh
##	latex   -interaction=batchmode  main.tex
##	mv main.dvi $(FILE).dvi
#	mv main.pdf $(FILE).pdf


#%.tex : %.rst 
#    cat rst2pdfNoErrors  | bash



spell:  
	for file in $(TEX_SOURCES); do \
	aspell -t -c $${file}; \
	done;



###SVN support
makeSVN:
	 svn mkdir  $(SVNREPO)/$(FILE) --username $(SVNUSER) ;\

dirs2SVN:
	- for dir in $(Dirs); do \
	svn mkdir  $(SVNREPO)/$(FILE)/$${dir} --username $(SVNUSER) --password ${SVNPSWD}  -m "Adding $${dir}"; \
	done;



Commit2SVN:
	 mkdir -p $(TMPDIR2SVN); \
	 cd  $(TMPDIR2SVN) ; \
	 svn co $(SVNREPO)/$(FILE); \
     ls ; \
     cd - ; \
     find  ./ -iname "*" | grep -v "*.svn*" |  egrep ".*count|.*gpg|.*rst2pdfNoErrors.*|.*cls|.*Makefile"  | xargs -I {} install -D {}  $(TMPDIR2SVN)/$(FILE)/{} ; \
     cd  $(TMPDIR2SVN)/$(FILE)  \
	 ls ;\
	 pwd ;\
	 svn status | grep '?'  |  egrep ".*count|.*gpg|.*rst2pdfNoErrors.*|.*cls|.*Makefile"   | sed 's/^.* /svn add --parents --force  --username $(SVNUSER) /' | bash ; \
	 svn ci ; \
	 cd $(CURDIR) ;\
	 rm -r  $(TMPDIR2SVN); 





add2Tgz:
	- tar -zcf $(FILE).tgz $(ALL_FILES) $(FILE).pdf

cleanGPG:
	- rm  ${shell find .  -iname "*.gpg" }  


clean :
	- mkdir -p $(TMPDIR)
	- mv *.dvi *.log *.loc *.lof *.toc *.ps *.aux *.bbl *.blg  *.bak $(TMPDIR)


doRST2PDF :
	- echo "RST to PDF"; cat rst2pdfNoErrors  | bash


checkOutSVN:
	 mkdir $(TMPDIR2SVN); \
	 cd  $(TMPDIR2SVN) ; \
	 svn co $(SVNREPO)/$(FILE); \
	 ls ;\
	 pwd ;\
	 cp -r ./ $(CURDIR); \
	 cd - ; 
	 rm -r  $(TMPDIR2SVN); 



encrypt:
	for file in $(TEX_SOURCES) $(FIGURES) $(BIBS) $(RST) $(MAKEFILE) $(SCRIPTS) $(SCRIPTS2) $(PYS); do \
    gpg --passphrase-fd 3 --batch -c $${file} 3< ./passwd.txt; \
    done;

decrypt:
	for file in ${shell find .  -iname "*.gpg" }; do \
    gpg --passphrase-fd 3  --batch   $${file} 3< ./passwd.txt; \
    done;
	 




##########Git support
makeGit:
	curl -u '$(GITUSER)' $(GITREPOPATH)  -d '{"name":"$(GITREPO)"}'

Commit2Git:
#	mkdir -p $(TMPDIR2GIT)/$(PROJECT) ;	cd  $(TMPDIR2GIT)/$(PROJECT) ; \
#	pwd ;
#	cd $(CURDIR) ; pwd ;
#	find  ./ -iname "*" | grep -v "*.svn*" | xargs -I {} install -D {}  $(TMPDIR2GIT)/$(PROJECT)/{} ; 
#	find  ./ -iname "*" | grep -v "*.svn*" | grep -v "*.git" | xargs -I {} cp -r {}  $(TMPDIR2GIT)/$(PROJECT) ; 
#	cd  $(TMPDIR2GIT)/$(PROJECT) ;	pwd ;
	- git init ;
	- git remote add origin git@github.com:$(GITUSER)/$(GITREPO).git ;
	- find  ./ -iname "*" | grep -v "*.svn*" |  egrep ".*count|.*gpg|.*rst2pdfNoErrors.*|.*cls|.*Makefile" | xargs -I {} git add {}
	- git commit -m "adding to GitHub.com"
	- git pull -s recursive -X ours origin master; 
#	git push -u origin master
	- git push https://github.com/$(GITUSER)/$(GITREPO).git master    
	- rm -rf ./.git





